﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using CoreEx.Common.Extensions;
using CoreEx.Common.Ioc;
using CoreEx.Services;
using LinFu.IoC.Configuration;
using LinFu.IoC.Interfaces;
using LinFu.IoC;
using Mono.Cecil;

namespace CoreEx.Common.Tests
{
    [Weaves("CoreEx.Samples.DomainModel.Implementation.dll")]
    public class SampleAssemblyWeaver : IAssemblyWeaver, IInitialize
    {
        public void Weave(AssemblyDefinition assemblyDefinition)
        {
            //Extract all the target types in the assembly.
            var targetTypes = assemblyDefinition.MainModule.Types.Cast<TypeDefinition>().Where(t => t.BaseType != null);
            
            var typesToImplement = new [] {typeof (INotifyPropertyChanged)};
            
            //Implement the interface for all target types
            targetTypes.ForEach(t => Implementor.Implement(t, typesToImplement));
        }

        public void Initialize(IServiceContainer source)
        {
            Implementor = source.GetService<IImplementor>();
        }

        public IImplementor Implementor { get; set; }
    }
}
